<?php

namespace app\admin\controller;

use app\admin\controller\Common;
use think\Request;
use app\admin\model\Users as UsersModel;
use think\Db;

class Users extends Common
{
   public function lst(){
        $users = new UsersModel();
        $useres = $users->users();
        // $ur = $users->render();
        // return dump($ur);
        // die;
        $this->assign(array(
         'useres' => $useres,
        ));
        return view();
   }

   public function status(){
      $id = input('id');
      $count = db('users')->where('status', 1)->count();
      $sh = Db::table('lan_users')->where(['status' => 1, 'id' => $id]) -> find();
      if ($sh){
         db('users')->update(['status' => 0 , 'id'=> $id]);
         return \json(['code' => 1 , 'msg' => '状态更改成功']);
      }else{
         db('users')->update(['status' => 1 , 'id'=> $id]);
         return \json(['code' => 1 , 'msg' => '状态更改成功']);
      }
  }

  public function del(){
      $id = input('id');
      $res = db('users')->delete(['id' => $id]);
      if ($res == '1') {
      $this->success('删除用户成功',url('lst'));
      }else{
      $this->error('删除用户失败');
      }
   }

   public function mess(){
      $id = input('id');
      $users = new UsersModel();
      $user_mess = $users->reply_mess($id);
      // return dump($user_mess);
      // die;
      $this->assign(array(
         'user_mess' => $user_mess,
        ));
      return view('users/mess');
   }
   
   public function del_mess(){
      $id = input('id');
      $res = db('messages')->delete(['id' => $id]);
      if($res){
          $this->success('删除评论成功',url('users/lst'));
      }else{
          $this->error('删除评论失败');
      }
   }

   public function reply(){
      if(request()->isPost()){
         $data = input('post.');
         $res = db('messages')->where('id', $data['id'])->update(['reply' => $data['reply']]);
         if($res){
            $this->success('评论留言成功',url('users/lst'));
         }
         else{
            $this->error('评论留言失败');
         }
      }
      $id = input('id');
      $res = db('messages')->where('id', $id)->find();
      $this->assign('mess', $res);
      return view();
   }
}
